package com.dragon.common.utils.html;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.dragon.common.constant.Constants;
import com.dragon.common.core.controller.BaseController;

public class HtmlUtil {
	public static List<JSONObject> htmlToJSONList(String html) throws ParseException {
		List<JSONObject> results = new ArrayList<>();
		JSONObject result;
		// Jsoup解析html
        Document document = Jsoup.parse(html);
        Elements trs = document.getElementsByTag("tr");
        for(Element tr : trs) {
        	String matchtime = tr.attr("data-matchtime");
        	if(matchtime==null || matchtime.isEmpty()) continue;
        	String matchdate = tr.attr("data-matchdate");
        	String StartTime = matchdate + " " + matchtime;
        	Elements divs = tr.getElementsByClass("betbtn-wait");
        	if(divs.size()==2) {
        		String d = divs.get(0).text();
        		String d1 = divs.get(0).text();
        		if(d!=null&&d1!=null&&d.equals("未开售")&&d.equals(d1)) continue;
        	}
        	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        	Date date = format.parse(StartTime+":00"); 
        	boolean isAfter = date.before(new Date());
        	if(isAfter) continue;
        	result = new JSONObject();
        	result.put("processname", tr.attr("data-processname"));
        	result.put("SerialNumber", tr.attr("data-matchnum"));
			result.put("StartTime", StartTime);
			result.put("LeagueName", tr.attr("data-simpleleague"));
			result.put("HomeField", tr.attr("data-homesxname"));
			result.put("OpponentField", tr.attr("data-awaysxname"));
			result.put("id", tr.attr("data-id"));
			results.add(result);
        }
        return results;
	}
	
	public static List<JSONObject> ecyhtmlToJSONList(String html) throws ParseException {
		List<JSONObject> results = new ArrayList<>();
		JSONObject result;
		// Jsoup解析html
        Document document = Jsoup.parse(html);
        Elements trs = document.getElementsByTag("tr");
        for(Element tr : trs) {
        	String matchtime = tr.attr("data-matchtime");
        	if(matchtime==null || matchtime.isEmpty()) continue;
        	String matchdate = tr.attr("data-matchdate");
        	String StartTime = matchdate + " " + matchtime;
        	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        	Date date = format.parse(StartTime+":00"); 
        	boolean isAfter = date.before(new Date());
        	if(isAfter) continue;
        	result = new JSONObject();
        	result.put("processname", tr.attr("data-processname"));
        	result.put("SerialNumber", tr.attr("data-matchnum"));
			result.put("StartTime", StartTime);
			result.put("LeagueName", tr.attr("data-simpleleague"));
			result.put("HomeField", tr.attr("data-homesxname"));
			result.put("OpponentField", tr.attr("data-awaysxname"));
			result.put("id", tr.attr("data-id"));
			results.add(result);
        }
        return results;
	}
	
	public static List<JSONObject> sizehtmlToJSONList(String html) throws ParseException {
		List<JSONObject> results = new ArrayList<>();
		JSONObject result;
		// Jsoup解析html
        Document document = Jsoup.parse(html);
        Elements trs = document.getElementsByTag("tr");
        String[] pageIds = HtmlUtil.getPageIds();
        for(Element tr : trs) {
        	String matchtime = tr.attr("data-matchtime");
        	if(matchtime==null || matchtime.isEmpty()) continue;
        	String matchdate = tr.attr("data-matchdate");
        	String StartTime = matchdate + " " + matchtime;
        	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        	Date date = format.parse(StartTime+":00"); 
        	boolean isAfter = date.before(new Date());
        	if(isAfter) continue;
        	result = new JSONObject();
        	String processname = tr.attr("data-processname");
        	String SerialNumber = tr.attr("data-matchnum");
        	result.put("processname", processname);
        	result.put("SerialNumber", SerialNumber);
			result.put("StartTime", StartTime);
			result.put("LeagueName", tr.attr("data-simpleleague"));
			result.put("HomeField", tr.attr("data-homesxname"));
			result.put("OpponentField", tr.attr("data-awaysxname"));
			result.put("id", tr.attr("data-id"));
			Elements jqs = tr.getElementsByAttributeValue("data-type", "jqs");
			for(String pageId : pageIds) {
        		if(pageId.indexOf(SerialNumber)<0) continue;
        		String id_ = pageId.split("\\^")[0];
        		List<List<String>> rqpk = getrqodds(id_);
        		List<JSONObject> EventHedgeProfitsSize = new ArrayList<>();
        		for(List<String> pk : rqpk) {
        			JSONObject r_ = new JSONObject();
        			for(int i=0; i<5; i++) {
        				Element jq = jqs.get(i);
        				Double jqv = Double.valueOf(jq.attr("data-sp"));
        				if(i==0) {
        					r_.put("OddsForOneTotalBalls", jqv);
        				}else if(i==1) {
        					r_.put("OddsForTwoTotalBalls", jqv);
        				}else if(i==2) {
        					r_.put("OddsForThreeTotalBalls", jqv);
        				}else if(i==3) {
        					r_.put("OddsForFourTotalBalls", jqv);
        				}else if(i==4) {
        					r_.put("OddsForFiveTotalBalls", jqv);
        				}
        			}
        			r_.put("Type", "大"+pk.get(1));
        			r_.put("OddsForPeripheryO2_5", Double.valueOf(pk.get(0)));
        			r_.put("BetMoneyForFiveTotalBalls", Constants.TotalExpense);
        			r_.put("BetMoneyForFourTotalBalls", Constants.TotalExpense);
        			r_.put("BetMoneyForOneTotalBalls", Constants.TotalExpense);
        			r_.put("BetMoneyForThreeTotalBalls", Constants.TotalExpense);
        			r_.put("BetMoneyForTwoTotalBalls", Constants.TotalExpense);
	        		r_.put("BetMoneyForPeripheryO2_5", Constants.TotalExpense);
	        		r_.put("BetMoneyJingCai", Constants.TotalExpense);
	        		r_.put("Income1", Constants.TotalExpense);
	        		r_.put("Profit", Constants.TotalExpense);
	        		r_.put("TotalExpense", Constants.TotalExpense);
        			EventHedgeProfitsSize.add(r_);
        		}
        		result.put("EventHedgeProfitsSize", EventHedgeProfitsSize);
        		break;
        	}
			results.add(result);
        }
        return results;
	}
	
	public static String[] getPageIds() {
		BaseController baseController = new BaseController();
		String aspUrl = "http://jc.win007.com/xml/bf_jc.txt?"+System.currentTimeMillis();
		String aspHtml = baseController.httpRequestToJSON(aspUrl, "GET", "utf-8");
		String[] aspHtmls = aspHtml.split("\\$");
		String[] results = aspHtmls[1].split("!");
		return results;
	}
	
	public static List<List<String>> getpkodds(String id) {
		String url = "http://vip.win007.com/AsianOdds_n.aspx?id=";
		url += (id+"&l=0");
		BaseController baseController = new BaseController();
		String html = baseController.httpRequestToJSON(url, "GET", "utf-8");
		Document document = Jsoup.parse(html);
        Elements trs = document.getElementsByTag("tr");
        List<List<String>> results = new ArrayList<>();
        boolean flag = false;
        for(Element tr : trs) {
        	Elements tds = tr.getElementsByTag("td");
        	for(Element td : tds) {
        		if(!flag) {
        			String name = td.text();
        			if(name==null||name.indexOf("Crown")<0) break;
            		flag = true;
            		Elements wholeLastOdds = tr.getElementsByAttributeValueEnding("oddstype", "wholeLastOdds");
            		List<String> oods = new ArrayList<>();
            		for(Element ood : wholeLastOdds) {
            			String ad = ood.text();
            			oods.add(ad);
            		}
            		results.add(oods);
            		break;
        		}else {
        			String name = tds.get(1).text();
        			if(name==null||name.indexOf("盘口")<0) return results;
        			flag = true;
            		Elements wholeLastOdds = tr.getElementsByAttributeValueEnding("oddstype", "wholeLastOdds");
            		List<String> oods = new ArrayList<>();
            		for(Element ood : wholeLastOdds) {
            			String ad = ood.text();
            			oods.add(ad);
            		}
            		results.add(oods);
            		break;
        		}
        	}
        	if(!flag) continue;
        }
		return results;
	}
	
	public static List<List<String>> getrqodds(String id) {
		String url = "http://vip.win007.com/OverDown_n.aspx?id=";
		url += (id+"&l=0");
		BaseController baseController = new BaseController();
		String html = baseController.httpRequestToJSON(url, "GET", "utf-8");
		Document document = Jsoup.parse(html);
        Elements trs = document.getElementsByTag("tr");
        List<List<String>> results = new ArrayList<>();
        boolean flag = false;
        for(Element tr : trs) {
        	Elements tds = tr.getElementsByTag("td");
        	for(Element td : tds) {
        		if(!flag) {
        			String name = td.text();
        			if(name==null||name.indexOf("Crown")<0) break;
            		flag = true;
            		Elements wholeLastOdds = tr.getElementsByAttributeValueEnding("oddstype", "wholeOdds");
            		List<String> oods = new ArrayList<>();
            		for(Element ood : wholeLastOdds) {
            			String ad = ood.text();
            			oods.add(ad);
            		}
            		results.add(oods);
            		break;
        		}else {
        			String name = tds.get(1).text();
        			if(name==null||name.indexOf("盘口")<0) return results;
        			flag = true;
            		Elements wholeLastOdds = tr.getElementsByAttributeValueEnding("oddstype", "wholeOdds");
            		List<String> oods = new ArrayList<>();
            		for(Element ood : wholeLastOdds) {
            			String ad = ood.text();
            			oods.add(ad);
            		}
            		results.add(oods);
            		break;
        		}
        	}
        	if(!flag) continue;
        }
		return results;
	}
}
